package com.zeoy.keeplivetestdemo.service;

import android.accessibilityservice.AccessibilityService;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import com.zeoy.keeplivetestdemo.aidl.KeepLiveAidlInterface;
import com.zeoy.keeplivetestdemo.utils.ServiceCheckUtils;

/**
 * Created by Administrator on 2018\3\23 0023.
 */

public class KeepAccessibilityService extends AccessibilityService {
    private Intent intent ;
    private Binder binder;
    private ServiceConnection connection;
    @Override
    public void onCreate() {
        super.onCreate();
        intent = new Intent(this,BackGroundService.class);
        binder = new KeepLiveAidlInterface.Stub() {
            @Override
            public String getServiceName() throws RemoteException {
                return "KeepAccessibilityService";
            }
        };
        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.e("zeoy", "【       辅助服务收到事件】");
        if (!ServiceCheckUtils.isServiceWorked(this, "com.zeoy.keeplivetestdemo.service.BackGroundService")) {
            Log.e("zeoy", "【辅助服务   重新   -  启动  - 后台】");
            startService(intent);
        }
    }

    @Override
    public void onInterrupt() {

    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.e("zeoy", "【       辅助服务开启】");
        if (!ServiceCheckUtils.isServiceWorked(this, "com.zeoy.keeplivetestdemo.service.BackGroundService")) {
            Log.e("zeoy", "【辅助服务   -  初次启动  - 后台】");
            startService(intent);
        }
       // bindService(intent, connection, BIND_IMPORTANT);
    }
}
